//
//  SetCarHelper.m
//  carcareIOS
//
//  Created by ileo on 15/6/29.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "SetCarHelper.h"
#import "AIUserInfo.h"
#import "UserHelper.h"
#import "NetCar.h"
#import "KVCHelper.h"

@interface SetCarHelper()

@property (nonatomic, copy) NSArray *models;
@property (nonatomic, copy) NSArray *years;
@property (nonatomic, copy) NSDictionary *brands;
@property (nonatomic, copy) NSDictionary *styles;

@property (nonatomic, strong) KVCHelper *kvcBrandID;
@property (nonatomic, strong) KVCHelper *kvcModelID;

@end

@implementation SetCarHelper

- (instancetype)init
{
    self = [super init];
    if (self) {
        if (USERINFO.carStyleID != 0) {
        }
    }
    return self;
}

-(void)getCarBrandSuccess:(void (^)(NSDictionary *))success{
    if (self.brands) {
        if (success) success(self.brands);
    }else{
        __weak __typeof(self) wself = self;
        [[[NetCar getCarBrandWithDrive:USERINFO.pastDevice] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
            NSDictionary *tmp = JSON[@"list"];
            if ([wself checkTheData:tmp]) {
                wself.brands = tmp;
                if (success) success(tmp);
            }
        }];
    }
}

-(void)getCarModelSuccess:(void (^)(NSArray *))success{
    if (self.models) {
        if (success) success(self.models);
    }else{
        if (self.carInfoModel.brandId != 0) {
            __weak __typeof(self) wself = self;
            [[[NetCar getCarModelWithDrive:USERINFO.pastDevice brandID:self.carInfoModel.brandId] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
                NSArray *tmp = JSON[@"items"];
                if ([wself checkTheData:tmp]) {
                    wself.models = tmp;
                    if (success) success(tmp);
                }
            }];
        }else{
            [AlertMgr showShortWarnTextTips:@[@"请先选择品牌"]];
        }
    }
}

-(void)getCarYearSuccess:(void (^)(NSArray *))success{
    if (self.years) {
        if (success) success(self.years);
    }else{
        if ([self.carInfoModel.modelName isNotNil]) {
            __weak __typeof(self) wself = self;
            [[[NetCar getCarStyleWithDrive:USERINFO.pastDevice modelID:self.carInfoModel.modelId] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
                NSDictionary *tmp = JSON[@"list"];
                if ([wself checkTheData:tmp]) {
                    wself.styles = tmp;
                    if (success) {
                        wself.years = [tmp allKeys];
                       success(wself.years);
                    }
                }
            }];
        }else{
            [AlertMgr showShortWarnTextTips:@[@"请先选择车系"]];
        }
    }
}

-(void)getCarStyleSuccess:(void (^)(NSArray *))success{
    if ([self.carInfoModel.year isNotNil] && self.styles) {
        if (success) success(self.styles[self.carInfoModel.year]);
    }else{
        [AlertMgr showShortWarnTextTips:@[@"请先选择车型年份"]];
    }
}

#pragma mark - 

-(BOOL)checkTheData:(id)data{
    BOOL ok = !([data isKindOfClass:[NSNull class]] || [data isKindOfClass:[NSString class]] || ([data isKindOfClass:[NSArray class]] && ((NSArray *)data).count == 0));
    if (!ok) {
        [AlertMgr showShortWarnTextTips:@[@"请先选择品牌"]];
    }
    return ok;
}

#pragma mark - 

-(void)setCarInfoModel:(CarInfoModel *)carInfoModel{
    _carInfoModel = carInfoModel;
    
    __weak __typeof(self) wself = self;
    self.kvcBrandID = [[KVCHelper alloc] initWithSubject:_carInfoModel forKeyPath:@"brandId" change:^{
        wself.carInfoModel.modelName = @"";
        wself.carInfoModel.modelId = 0;
        wself.models = nil;
    }];
    
    self.kvcModelID = [[KVCHelper alloc] initWithSubject:self.carInfoModel forKeyPath:@"modelId" change:^{
        wself.carInfoModel.styleName = @"";
        wself.carInfoModel.carTypeId = 0;
        wself.carInfoModel.year = @"";
        wself.years = nil;
        wself.styles = nil;
    }];

}

-(void)setYears:(NSArray *)years{
    _years = [years sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return -[obj1 compare:obj2 options:NSNumericSearch];
    }];
}

@end
